package com.medical.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.medical.model.entity.Dept;
import com.medical.model.ro.DeptRo;
import com.medical.service.DeptService;
import com.medical.domain.R;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author TISNIW
 * @version 1.0
 * @desc
 */
@RestController
@RequestMapping("/dept")
public class DeptController {

    @Resource
    private DeptService deptService;

    // 新增部门
    @PostMapping
    public R<Boolean> addDept(@RequestBody Dept dept) {
        return R.ok(deptService.save(dept));
    }

    // 删除部门
    @DeleteMapping("/{id}")
    public R<Boolean> deleteDept(@PathVariable Long id) {
        return R.ok(deptService.removeById(id));
    }

    // 更新部门信息
    @PutMapping
    public R<Boolean> updateDept(@RequestBody Dept dept) {
        return R.ok(deptService.updateById(dept));
    }

    // 查询单个部门
    @GetMapping("/{id}")
    public R<Dept> getDept(@PathVariable Long id) {
        return R.ok(deptService.getById(id));
    }

    // 分页查询所有部门
    @GetMapping("/list")
    public R<Page<Dept>> listDepts(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        return R.ok(deptService.page(new Page<>(pageNum, pageSize)));
    }
    @GetMapping("/tree")
    public R<List<DeptRo>> getDeptTree() {
        List<Dept> allDepts = deptService.list();
        List<DeptRo> tree = deptService.buildDeptTree(allDepts);
        return R.ok(tree);
    }
}
